﻿using System.Collections.Generic;
using UserRoles.Models;

namespace UserRoles.Services
{
    /// <summary>
    /// In memory UserRoleService
    /// After restarting server,
    /// already registered users won't have roles
    /// </summary>
    public class UserRoleService
    {
        private static readonly UserRoleService Instance = new UserRoleService();
        private static readonly Dictionary<string, UserRole> Roles = new Dictionary<string, UserRole>();
        
        private UserRoleService()
        {
        }

        public static UserRoleService GetInstance()
        {
            return Instance;
        }

        public void AddRoleToUser(string userName, UserRole role)
        {
            if (Roles.ContainsKey(userName))
                Roles[userName] = role;
            Roles.Add(userName, role);
        }

        public UserRole GetRole(string userName)
        {
            if (!Roles.ContainsKey(userName))
                return UserRole.User;
            return Roles[userName];
        }

        public string GetRolesAsString(string userName)
        {
            return GetRole(userName).ToString("G");
        }
    }
}